package net.kts.subskill.toilet;



import java.util.List;

import net.kts.define.SQLiteDatabaseStr;
import net.kts.location.MyLocationManager;
import net.kts.main.R;
import net.kts.subskill.toilet.model.ToiletVO;
import net.kts.subskill.toilet.overlay.MyItemizedOverlay;
import net.kts.subskill.toilet.sqlite.ToiletDaoSQLite;
import net.kts.transrate.TranslateService;
import net.kts.util.url.mylocation.MyLocationDrawable;

import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class ToiletSearch extends MapActivity {

	private MapView mapView = null;
//	private LocationManager locationManager = null;
//	private MyLocationOverlay myLocationOverlay = null;
//	private LocationListener locationListenerNet = null;
//	private LocationListener locationListenerGps = null;
	private LocationListener toiletLocationListener = null;

	private ToiletDaoSQLite toiletDaoSQLite = null;
	private Handler uiCallback =  null;
	private MyLocationDrawable myLocationDrawable = null;

	@Override
	protected void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		super.onCreate(bundle);
		setContentView(R.layout.subskill_toilet_map);
		
//		if(bundle != null) {
//			ToiletVO toiletVO = (ToiletVO)bundle.getSerializable("huni");
//		}
//		

		
		
		init();
		
//		this.toiletLocationListener = createListner();

		this.mapView = (MapView)findViewById(R.id.toiletMap);
		this.mapView.getController().setZoom(19);
		this.mapView.setBuiltInZoomControls(true);

//		this.locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//
//		this.myLocationOverlay = new MyLocationOverlay(this, this.mapView);
//
//
//		this.myLocationOverlay = new MyLocationOverlay(this, this.mapView);
//		this.mapView.getOverlays().add(this.myLocationOverlay);

		
		

//		this.locationListenerGps = createListner();
//		this.locationListenerNet = createListner();


//		Criteria criteria = new Criteria();
//		criteria.setAccuracy(Criteria.ACCURACY_FINE);
//		criteria.setAltitudeRequired(false);
//		criteria.setBearingRequired(false);
//		criteria.setCostAllowed(true);
//		criteria.setPowerRequirement(Criteria.POWER_LOW);



//		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

	}

	private void init() {

//		String pakageName = "net.kts.subskill.toilet";
//		String dbFileName = "kts001.db";
//		String assertPath = "koreaTravelStory.db3";
//
//		MoveDatabase md = new MoveDatabase(this, pakageName, dbFileName, assertPath);
//		md.assertToData();

		this.toiletDaoSQLite = new ToiletDaoSQLite(this, SQLiteDatabaseStr.dbFileName, null, 1);



		this.uiCallback = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);

				Log.d("kts", "toiletHandler");

				Bundle bundle =  msg.getData();

				double lat = Double.valueOf((String)bundle.get("lat"));
				double lng = Double.valueOf((String) bundle.get("lng"));

				Log.d("kts", "toiletHandler lat : " + lat + " lng : " + lng);

				List<ToiletVO> toiletVList = toiletDaoSQLite.selectCurrentPositionToiletList(lat, lng);

				//				Log.d("kts", "toiletHandler toiletList result count : " + toiletVList.size());

				
				GeoPoint point = null;
				ToiletVO toiletVO = null;

				if(toiletVList != null) {
					
					Drawable drawable = getResources().getDrawable(R.drawable.toilet);
					MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(drawable, mapView);

					for(int indexI = 0 ; indexI< toiletVList.size(); ++indexI) {
						//					Log.d("kts", " toilet count : " + toiletVList.size());
						//					Log.d("kts", " toilet index : " + indexI + " value : " + toiletVList.get(indexI).toString());

						toiletVO = toiletVList.get(indexI);
						point = new GeoPoint((int)(toiletVO.getLat() * 1000000), (int)(toiletVO.getLng() * 1000000));

						OverlayItem overlayItem = new OverlayItem(point, toiletVO.getName(), TranslateService.translateRepository(toiletVO.getType()) + "||" + TranslateService.translateRepository(toiletVO.getDisablePerson()));
						itemizedOverlay.addOverlay(overlayItem);

					}

					if(itemizedOverlay.size() >0) {

						mapView.getOverlays().add(itemizedOverlay);
					}


				}

			}

		};
		
		
		Drawable drawable = getResources().getDrawable(R.drawable.story_flag_my);
		this.myLocationDrawable = new MyLocationDrawable(drawable);
		

		
	}
	
	
	private LocationListener createListner() {
		LocationListener locationListener = new LocationListener() {

			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				updateWidthNewLocation(null);
			}

			@Override
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				updateWidthNewLocation(location);
			}
		};
		
		return locationListener;
	}


	public void updateWidthNewLocation(Location location){

		if(location !=null){    		
			Double geoLat = location.getLatitude()*1000000;
			Double geoLng = location.getLongitude()*1000000;
			GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
			
			this.myLocationDrawable.updateOverlay(new OverlayItem(point, "", ""));
			this.mapView.getOverlays().add(myLocationDrawable);

			this.mapView.getController().animateTo(point);    		
		}
	}




	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(this.toiletLocationListener == null) {
			this.toiletLocationListener= createListner();
			MyLocationManager.addLocationListener(this.toiletLocationListener);
		}
		
		Bundle bundle = new Bundle();
		
		
		Location location = MyLocationManager.giveMeMyCurrentLocation();
		
		bundle.putString("lat", String.valueOf(location.getLatitude()));
		bundle.putString("lng", String.valueOf(location.getLongitude()));
		
		Message msg = new Message();
		msg.setData(bundle);
		
		this.uiCallback.sendMessage(msg);
		
		GeoPoint point = new GeoPoint((int)(location.getLatitude() * 1000000), (int)(location.getLongitude()* 1000000 ));
		
		this.myLocationDrawable.updateOverlay(new OverlayItem(point, "", ""));
		this.mapView.getOverlays().add(myLocationDrawable);
		
		this.mapView.getController().animateTo(point);    
		
//		this.myLocationOverlay.enableCompass();
//		this.myLocationOverlay.enableMyLocation();
//		
//		String provider = locationManager.getBestProvider(BestProvider.getBestProviderCriteria(), true);
//		Log.d("kts_locationProvider", "Toilet Search : " +provider);
		
//		Location location = locationManager.getLastKnownLocation(provider);
//		updateWidthNewLocation(location);

//		this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this.locationListenerGps);
//		this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this.locationListenerNet);
//		this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this.locationListener);

//		this.myLocationOverlay.enableMyLocation();
//
//		this.myLocationOverlay.runOnFirstFix(new Runnable() {
//
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				mapView.getController().setCenter(myLocationOverlay.getMyLocation());
//
//				double lat = myLocationOverlay.getMyLocation().getLatitudeE6() * 0.000001;
//				double lng = myLocationOverlay.getMyLocation().getLongitudeE6() * 0.000001;
//
//				Log.d("kts", "lat : " + lat  + " lng : " + lng );
//
//				Location locationA = new Location("point A");
//
//				locationA.setLatitude(lat);
//				locationA.setLongitude(lng);
//
//				Location locationB = new Location("point B");
//
//				locationB.setLatitude(37.484437);
//				locationB.setLongitude(127.017462);
//
//				double distance = locationA.distanceTo(locationB);
//
//				Log.d("kts", "distance : " + distance);
//
//				Message msg = new Message();
//				Bundle bundle = new Bundle();
//				bundle.putString("lat", String.valueOf(lat));
//				bundle.putString("lng", String.valueOf(lng));
//				
//				
//				
//				msg.setData(bundle);
//
//				uiCallback.sendMessage(msg);
//
//			}
//		});

	}
	
	


	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
//		this.locationManager.removeTestProvider("gps");
//		this.locationManager.removeUpdates(this.locationListenerGps);
//		this.locationManager.removeUpdates(this.locationListenerNet);
//		this.locationManager.remo
//		this.myLocationOverlay.disableCompass();
//		this.myLocationOverlay.disableMyLocation();
		
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		
//		ToiletVO toiletVO = new ToiletVO();
//		
//		toiletVO.setDisablePerson("fuck");
//		toiletVO.setId("sexyme");
//		
//		outState.putSerializable("huni", toiletVO);
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
